﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Xrm.Solutions.FrameworkSample.Common;
using Microsoft.Xrm.Sdk;

namespace Xrm.Solutions.FrameworkSample.UnitTests
{
    [TestClass]
    public class TestCreditLimitCRM
    {
        [TestMethod]
        public void TestCreditLimitUsingCRMDI()
        {
            Guid contactId = Guid.NewGuid();
            
            IIdentityCheckService identityService
                = new Xrm.Solutions.FrameworkSample.Common.Fakes.StubIIdentityCheckService()
                {
                    RunCheckStringStringDateTimeString = (firstname, lastname, dob, address) =>
                    {
                        if (firstname == "Wael")
                            return 99;
                        else
                            return 60;
                    }
                };

            IOrganizationService crmService
                = new Microsoft.Xrm.Sdk.Fakes.StubIOrganizationService()
                {
                    RetrieveStringGuidColumnSet = (entityName, entityId, columnset) =>
                        {
                            if (entityId == contactId)
                            {
                                Entity contact = new Entity("contact");
                                contact["firstname"] = "Wael";
                                contact["lastname"] = "Hamze";
                                contact["birthdate"] = DateTime.Now;
                                contact["address"] = "London";
                                contact["xrm_currentlimit"] = 500;

                                return contact;
                            }
                            throw new Exception("Entity Not Found");
                        }
                };

            CreditLimitServiceCRM creditService
                = new CreditLimitServiceCRM(identityService, crmService);

            int agreedLimit = creditService.IncreaseLimit(contactId, 2000);

            Assert.AreEqual(750, agreedLimit);
        }
    }
}
